<?php
declare (strict_types = 1);

namespace app\admin\controller\cloth;

use think\facade\Request;
use app\common\service\ClothTemp as S;
use app\common\model\ClothTemp as M;
use app\common\model\ClothMain as CLOTH;
use app\common\model\ClothType as CT;
use app\common\model\ClothColor as COLOR;
use app\common\model\SupplierMain as SUP;
use app\common\model\CustomerSample as SAMP_M;
use app\common\model\SystemConfig as CONFIG;
use QL\QueryList;

class Temp extends  \app\admin\controller\Base
{
	
    protected $middleware = ['AdminCheck','AdminPermission'];
	
	//初始化
	public function initialize(){
		parent::initialize();
		
		$this->assign('controller_title','临时面料');
	}

    // 列表
    public function index(){
        if (Request::isAjax()) {
            return $this->getJson(M::getList([['uuid','=',$this->uuid]]));
        }
		return $this->fetch('',[
			'type_list'=>CT::getList(['uuid'=>$this->uuid]),
		]);
    }

    // 添加
    public function add(){
        if (Request::isAjax()) {
			//追加uuid
			$data=array_merge(Request::post(),['uuid'=>$this->uuid]);
            return $this->getJson(S::goAdd($data));
        }
        return $this->fetch('edit',[
			'action_up'=>false,
			'type_list'=>CT::listData(['uuid'=>$this->uuid]),
			'supplier'=>SUP::listData(['uuid'=>$this->uuid]),
			'options'=>config_cache_file('options'),
			'component'=>config_cache_file('component'),
		]);
    }

    // 编辑
    public function edit($id){
        if (Request::isAjax()) {
			$data=array_merge(Request::post(),['uuid'=>$this->uuid]);
            return $this->getJson(S::goEdit($data,['id'=>$id,'uuid'=>$this->uuid]));
        }
		
		$model=M::info(['uuid'=>$this->uuid,'id'=>$id]);
		if(!$model) $this->getJson(102);
        
		return $this->fetch('',[
			'action_up'=>true,
			'model' => $model,
			'type_list'=>CT::listData(['uuid'=>$this->uuid]),
			'supplier'=>SUP::listData(['uuid'=>$this->uuid]),
			'options'=>config_cache_file('options'),
			'component'=>config_cache_file('component'),
		]);
    }

    // 状态
    public function status($id){
        return $this->getJson(S::goStatus(Request::post(),['id'=>$id,'uuid'=>$this->uuid]));
    }

    // 删除
    public function remove($id){
        return $this->getJson(S::goRemove(['id'=>$id,'uuid'=>$this->uuid]));
    }

    // 批量删除
    public function batchRemove(){
        return $this->getJson(S::goBatchRemove([
				['id','in',Request::post('ids')],
				['uuid','=',$this->uuid],
			]));
    }

    // 回收站
    public function recycle(){
        if (Request::isAjax()) {
            return $this->getJson(S::goRecycle([['uuid','=',$this->uuid]]));
        }
        return $this->fetch();
    }
    
    // 设置某个值
    public function setValue(){
        if (Request::isAjax()) {
            $type=I('post.type','');
            if($type=='settype'){           //批量设置分类
                return $this->getJson(S::setTypes(Request::post(),$this->uuid)); 
            }else{
                return $this->getJson(S::setFieldValue(Request::post(),['id'=>I('post.id'),'uuid'=>$this->uuid])); 
            }
        }
        return $this->fetch();
    }
	
	// 复制产品
	public function copyCloth(){
		if (Request::isAjax()) {
			$id=I('post.ids','');
			$id=explode('|',$id);
			return $this->getJson(S::copy($id,$this->uuid));
		}else{
			return $this->getJson(404);
		}
	}
	
	//导入产品进入面料库
	public function import(){
		if(Request::isAjax()){
			$id=I('post.id');
			$T=M::find($id)->toArray();
			unset(
				$T['id'],
				$T['update_time'],
				$T['create_time'],
				$T['delete_time']
			);
			CLOTH::create($T);
			S::setFieldValue(['field'=>'isimport','value'=>1],['id'=>$id,'uuid'=>$this->uuid]);
			return $this->getJson(['msg'=>'导入成功！']);
		}
	}
	
	//生成不重复的随机编号
	public function randNumber(){
		if(Request::isAjax()){
			$number=rand(10001,99999);
			return $this->getJson(['data'=>['number'=>$number]]);
		}
	}
	
	// 差价表
	public function cost(){
	    if (Request::isAjax()) {
	        return $this->getJson(M::costList(['status'=>1,'uuid'=>$this->uuid],['sort'=>'asc']));
	    }
		
		return $this->fetch('',[
			'more'=>I('get.more',0),
		]);
	}
	
	//报价表
	public function table(){
		if (Request::isAjax()) {
		    return $this->getJson(M::tableList(['m.status'=>1,'m.wx_isshow'=>1,'m.uuid'=>$this->uuid]));
		}
		
		return $this->fetch('',[
			'more'=>I('get.more',0),
			'today'=>date('Y年m月d日'),
		]);
	}
	
	// 产品标签打印
	public function label(){
		$id=I('id','');
		$type=I('type','cloth');
		
		if($id==="") $this->error('页面访问有误~！');
		
		if($type === "cloth"){				//打印产品标签
			$id=explode('|',$id);
			$T=M::field('id,title,number,kgm,breadth,square,component,tube,emptykg,side_type')->where([
				['id','in',$id],
				['uuid','=',$this->uuid]
			])->select()->toArray();
		}else if($type === "sample"){		//色卡登记打印
			$card=SAMP_M::where(['id'=>$id,'uuid'=>$this->uuid])->value('card');
			if($card!=''){
				// $new_card=$this->replce_number($card,1);		//替换编号对应系统编号
				$new_card=$card;
				$card=explode('/', $new_card['card']);
				$T=M::field('id,title,number,kgm,breadth,square,component,tube,emptykg,side_type')->where([
					['number','in',$card],
					['uuid','=',$this->uuid]
				])->select()->toArray();
			}else{
				$this->error('没数据可以生成~！');
			}
		}else{
			$this->error('非法访问~！');
		}
		// 替换
		// $T=$this->replce_number(['data'=>$T,'replce'=>$new_card['replce']],2);
		
		$print=CONFIG::get('info.print');
		
		return $this->fetch('cloth/main/label',[
			'T'=>$T,'count'=>count($T),
			'print'=>$data=$print,
			'print_data1'=>json_encode(M::printData($T,$print,'FabricLabel1')),
			'print_data2'=>json_encode(M::printData($T,$print,'FabricLabel2')),
			'print_data3'=>json_encode(M::printData($T,$print,'FabricLabel3')),
		]);
	}
}
